// compontent/bindPhoneModal/bindPhoneModal.js
const app = getApp(); //实例化appjs
var countCodeTimeTask = ''; //验证码计时任务
var util = require('../../utils/util.js'); //工具类
var http = require('../../service/http.js'); //请求类
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    //是否打开模态框
    showBindPhoneModel: {
      type: Boolean,
      value: false,
    },
    phoneNum:{
      type:Number,
      value:''
    },
    isPhone:{
      type: Boolean,
      value: false,
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    // isPhone: '', //是否是手机号
    // phoneNum: '', //手机号码
    codeTime: 60, //计时
    sendCode: false, //是否已经触发发送验证码
    getCode: false, //是否已经获取验证码
    codeNum: '', //验证码
    validataForm: false, //验证表单

  },

  /**
   * 组件的方法列表
   */
  methods: {

    //关闭绑定手机模态框
    closeModel: function() {
      console.log("停止倒计时 getCode:false");
      clearTimeout(countCodeTimeTask);
      this.setData({
        showBindPhoneModel: false,
        getCode: false, //还未获取验证码
        sendCode: false,//是否点击发送
        codeTime: 60, //计时
      })
      this.triggerEvent('closeModel', {
        showBindPhoneModel: false
      })
    },

    //验证手机号码
    testPhone: function(e) {
      let [phoneNum, isPhone] = [e.detail.value, this.data.isPhone];
      if (phoneNum.length >= 11) {
        if (!(/^1[23456789]\d{9}$/.test(phoneNum))) {
          isPhone = false;
          util.showToast('手机号有误', 1500, 'none');
        } else {
          isPhone = true;
        }
      } else {
        isPhone = false;
      }
      this.setData({
        isPhone: isPhone,
        phoneNum: phoneNum
      })
    },


    //获取验证码
    getPhoneCode: function(e) {
      
      this.setData({
        codeTime: 60, //计时
      })
      this.getPhoneCode();
    },

    //获取验证码
    getPhoneCode: function() {
      console.log('手机号码：' + this.data.phoneNum);
      let [phoneNum, getCode, sendCode] = [this.data.phoneNum, '', true];
      http.getReq('/weixin/personal/getVerifyCode', {
        phoneNum: phoneNum
      }, (res) =>{
        console.log('获取验证码回调', res)

        if (res.ret) {
          getCode = true;
          this.countTime();
        } else {
          getCode = false;
          sendCode = false;
          util.showToast(res.msg, 1500, 'none', ()=> {}); //弹窗
        }
        this.setData({
          codeNum: res.data,
          getCode: getCode,
          sendCode: sendCode
        })

      });
    },
    //倒计时时间
    countTime: function() {
      
      console.log("倒计时---" + this.data.codeTime);
      let [codeTime] = [ this.data.codeTime]
      codeTime--;
      if (codeTime == 0) {
        console.log("停止倒计时 getCode:false");
        clearTimeout(countCodeTimeTask);
        this.setData({
          getCode: false,
          sendCode: false,
          codeTime: 60 //初始化验证码过期时间
        })
        return
      } else {
        this.setData({
          codeTime: codeTime
        })
        countCodeTimeTask = setTimeout(()=> {
          this.countTime();
        }, 1000)
      }

    },
    //-提交表单
    savePhoneNum: function(e) {
      // console.log("提交表单savePhoneNum", e);
      this.validateSavePhoneForm(e); //验证表单是否合法
      let validataForm = this.data.validataForm; //验证表单结果
      if (validataForm) {
        this.saveForm(e);
      }
    },
    //验证表单
    validateSavePhoneForm: function(e) {
      console.log('validateSavePhoneForm',this.data);
      let [code, isPhone, codeNum] = [e.detail.value.code, this.data.isPhone, this.data.codeNum];
      let validataForm = false;
      //验证手机号和验证码是否合法
      if (!isPhone) {
        util.showToast('手机号输入不合法', 1500, 'none');
      } else if (code.length != 4) {
        util.showToast('验证码输入不合法', 1500, 'none');
      } else if (codeNum != code) {
        util.showToast('验证码输入错误', 1500, 'none');
      } else {
        validataForm = true;
      }
      this.setData({
        validataForm: validataForm
      })

    },
    //提交表单保存手机号
    saveForm: function(e) {
      console.log(e);
      let [code, phoneNum,isPhone] = [e.detail.value.code, e.detail.value.phone,this.data.isPhone];
      let rdSession = wx.getStorageSync('rd_session');
      let validataForm = this.data.validataForm

      if (code.length == 4 && validataForm == true) {
        http.getReq('/weixin/personal/verifyPhoneNum', {
          phoneNum: phoneNum,
          rdSession: rdSession,
          code: code
        }, (res)=> {

          clearTimeout(countCodeTimeTask); //结束计时
          console.log('绑定手机号回调', res);

          let showBindPhoneModel = this.data.showBindPhoneModel;

          if (res.ret) { //验证通过
            util.showToast('绑定成功', 1000, 'success');
            showBindPhoneModel = false;
            wx.setStorageSync('isBindPhone', true) //已绑定手机
            this.triggerEvent('savePhone', {
              showBindPhoneModel: false,
              isBindPhone: true
            });

          } else { //验证未通过
            showBindPhoneModel = true
            util.showToast(res.msg, 1000, 'none');
          }

          this.setData({
            showBindPhoneModel: showBindPhoneModel,
            codeTime: 60,
            getCode: false,
            sendCode: false
          })
        });
      } else {
        util.showToast('验证码输入不合法', 1000, 'none');
        return
      }
    },
  }
})